<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>WebSocket | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C09-IM/SocketIO.html" />
    
    
    <link rel="prev" href="../C09-IM/AboutIM.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="9.2"
        data-chapter-title="WebSocket"
        data-filepath="C09-IM/WebSocket.md"
        data-basepath=".."
        data-revision="Mon May 27 2019 03:35:45 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-Introduction/index.html">
            
                
                    <a href="../C01-Introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        项目介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-Introduction/AboutToutiaoWebVM.html">
            
                
                    <a href="../C01-Introduction/AboutToutiaoWebVM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        ToutiaoWeb虚拟机使用说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-Introduction/PycharmRemoteDevelop.html">
            
                
                    <a href="../C01-Introduction/PycharmRemoteDevelop.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Pycharm远程开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-Introduction/AboutProject.html">
            
                
                    <a href="../C01-Introduction/AboutProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        产品与开发
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-Database/index.html">
            
                
                    <a href="../C02-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-Database/DatabaseDesign.html">
            
                
                    <a href="../C02-Database/DatabaseDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-Database/ORM.html">
            
                
                    <a href="../C02-Database/ORM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        理解ORM
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C02-Database/SQLAlchemyMapping.html">
            
                
                    <a href="../C02-Database/SQLAlchemyMapping.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        SQLAlchemy映射构建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C02-Database/SQLAlchemyOperations.html">
            
                
                    <a href="../C02-Database/SQLAlchemyOperations.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        SQLAlchemy操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="C02-Database/DatabaseTheory.html">
            
                
                    <a href="../C02-Database/DatabaseTheory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        数据库理论
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="C02-Database/DistributedID.html">
            
                
                    <a href="../C02-Database/DistributedID.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        分布式ID
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="C02-Database/DatabaseOptimize.html">
            
                
                    <a href="../C02-Database/DatabaseOptimize.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        数据库优化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="C02-Database/Redis.html">
            
                
                    <a href="../C02-Database/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        Redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Gitflow/index.html">
            
                
                    <a href="../C03-Gitflow/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Git工作流
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Gitflow/Gitflow.html">
            
                
                    <a href="../C03-Gitflow/Gitflow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Gitflow
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Gitflow/AboutGit.html">
            
                
                    <a href="../C03-Gitflow/AboutGit.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Git总结
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Gitflow/ProjectDirectory.html">
            
                
                    <a href="../C03-Gitflow/ProjectDirectory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        工程目录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Gitflow/DebugMethod.html">
            
                
                    <a href="../C03-Gitflow/DebugMethod.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        调试方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-JWT/index.html">
            
                
                    <a href="../C04-JWT/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        JWT
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-JWT/JWT.html">
            
                
                    <a href="../C04-JWT/JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        JWT &amp; JWS &amp; JWE
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-JWT/PythonLib.html">
            
                
                    <a href="../C04-JWT/PythonLib.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        PythonLib
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-JWT/Implement.html">
            
                
                    <a href="../C04-JWT/Implement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        实施方案
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-JWT/Forbbiden.html">
            
                
                    <a href="../C04-JWT/Forbbiden.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        禁用问题
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-OSS/index.html">
            
                
                    <a href="../C05-OSS/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象存储
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-OSS/Qiniu.html">
            
                
                    <a href="../C05-OSS/Qiniu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        七牛云存储
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-OSS/CDN.html">
            
                
                    <a href="../C05-OSS/CDN.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        CDN
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C06-Cache/index.html">
            
                
                    <a href="../C06-Cache/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        缓存
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C06-Cache/CacheArchitecture.html">
            
                
                    <a href="../C06-Cache/CacheArchitecture.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        缓存的架构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C06-Cache/CacheData.html">
            
                
                    <a href="../C06-Cache/CacheData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C06-Cache/CacheTTLAndEviction.html">
            
                
                    <a href="../C06-Cache/CacheTTLAndEviction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        缓存的有效期与淘汰策略
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C06-Cache/CachePatterns.html">
            
                
                    <a href="../C06-Cache/CachePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        缓存模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C06-Cache/CacheProblems.html">
            
                
                    <a href="../C06-Cache/CacheProblems.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        缓存问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C06-Cache/ProjectCacheAndStorageDesign.html">
            
                
                    <a href="../C06-Cache/ProjectCacheAndStorageDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        项目缓存与Redis持久存储设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="C06-Cache/ProjectCacheImplement.html">
            
                
                    <a href="../C06-Cache/ProjectCacheImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        项目缓存实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="C06-Cache/ProjectRedisStorageImplement.html">
            
                
                    <a href="../C06-Cache/ProjectRedisStorageImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        项目Redis持久存储实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C07-APScheduler/index.html">
            
                
                    <a href="../C07-APScheduler/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        APScheduler
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C07-APScheduler/APSchedulerTutorial.html">
            
                
                    <a href="../C07-APScheduler/APSchedulerTutorial.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        APScheduler使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C07-APScheduler/FixStatistics.html">
            
                
                    <a href="../C07-APScheduler/FixStatistics.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定时修正统计数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C08-RPC/index.html">
            
                
                    <a href="../C08-RPC/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        RPC
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C08-RPC/AboutRPC.html">
            
                
                    <a href="../C08-RPC/AboutRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        RPC简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C08-RPC/gRPC.html">
            
                
                    <a href="../C08-RPC/gRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        gRPC
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="C08-RPC/ProtocolBuffers.html">
            
                
                    <a href="../C08-RPC/ProtocolBuffers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        Protocol Buffers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C08-RPC/RecoSysIDL.html">
            
                
                    <a href="../C08-RPC/RecoSysIDL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        推荐系统接口定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C08-RPC/Server.html">
            
                
                    <a href="../C08-RPC/Server.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        服务端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="C08-RPC/Client.html">
            
                
                    <a href="../C08-RPC/Client.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        客户端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.7" data-path="C08-RPC/ToutiaoAPIViewImplement.html">
            
                
                    <a href="../C08-RPC/ToutiaoAPIViewImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.7.</b>
                        
                        头条首页新闻推荐接口编写
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C09-IM/index.html">
            
                
                    <a href="../C09-IM/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        即时通信
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C09-IM/AboutIM.html">
            
                
                    <a href="../C09-IM/AboutIM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        即时通信简介
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="9.2" data-path="C09-IM/WebSocket.html">
            
                
                    <a href="../C09-IM/WebSocket.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C09-IM/SocketIO.html">
            
                
                    <a href="../C09-IM/SocketIO.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        Socket.IO
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C09-IM/Chat.html">
            
                
                    <a href="../C09-IM/Chat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        头条聊天服务实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C09-IM/MessagePush.html">
            
                
                    <a href="../C09-IM/MessagePush.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        头条在线消息推送实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="C10-Elasticsearch/index.html">
            
                
                    <a href="../C10-Elasticsearch/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Elasticsearch
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="C10-Elasticsearch/Introduction.html">
            
                
                    <a href="../C10-Elasticsearch/Introduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        简介与原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="C10-Elasticsearch/ConceptAndCluster.html">
            
                
                    <a href="../C10-Elasticsearch/ConceptAndCluster.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        概念与集群
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="C10-Elasticsearch/AnalysisIK.html">
            
                
                    <a href="../C10-Elasticsearch/AnalysisIK.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        IK中文分析器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="C10-Elasticsearch/IndexAndType.html">
            
                
                    <a href="../C10-Elasticsearch/IndexAndType.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        索引与类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="C10-Elasticsearch/Document.html">
            
                
                    <a href="../C10-Elasticsearch/Document.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        文档
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="C10-Elasticsearch/Logstash.html">
            
                
                    <a href="../C10-Elasticsearch/Logstash.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        Logstash导入数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="C10-Elasticsearch/Query.html">
            
                
                    <a href="../C10-Elasticsearch/Query.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.8" data-path="C10-Elasticsearch/ToutiaoSearchImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSearchImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.8.</b>
                        
                        头条全文检索实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.9" data-path="C10-Elasticsearch/Suggest.html">
            
                
                    <a href="../C10-Elasticsearch/Suggest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.9.</b>
                        
                        suggest查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.10" data-path="C10-Elasticsearch/ToutiaoSuggestImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSuggestImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.10.</b>
                        
                        头条suggest查询实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="C11-Unittest.html">
            
                
                    <a href="../C11-Unittest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="C12-Deployment/index.html">
            
                
                    <a href="../C12-Deployment/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        部署相关
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="12.1" data-path="C12-Deployment/Gunicorn.html">
            
                
                    <a href="../C12-Deployment/Gunicorn.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.1.</b>
                        
                        Gunicorn
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="C12-Deployment/Supervisor.html">
            
                
                    <a href="../C12-Deployment/Supervisor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.2.</b>
                        
                        Supervisor
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="websocket">WebSocket</h1>
<p><strong>HTML5&#x5B9A;&#x4E49;&#x4E86;WebSocket&#x534F;&#x8BAE;</strong>&#xFF0C;&#x80FD;&#x66F4;&#x597D;&#x7684;&#x8282;&#x7701;&#x670D;&#x52A1;&#x5668;&#x8D44;&#x6E90;&#x548C;&#x5E26;&#x5BBD;&#xFF0C;&#x5E76;&#x4E14;&#x80FD;&#x591F;&#x66F4;&#x5B9E;&#x65F6;&#x5730;&#x8FDB;&#x884C;&#x901A;&#x8BAF;&#x3002;</p>
<p>&#x5728;2008&#x5E74;&#x8BDE;&#x751F;&#xFF0C;2011&#x5E74;&#x6210;&#x4E3A;&#x56FD;&#x9645;&#x6807;&#x51C6;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x57FA;&#x672C;&#x6240;&#x6709;&#x6D4F;&#x89C8;&#x5668;&#x90FD;&#x5DF2;&#x7ECF;&#x652F;&#x6301;&#x4E86;&#x3002;</p>
<p>WebSocket&#x662F;&#x4E00;&#x79CD;&#x5728;&#x5355;&#x4E2A;TCP&#x8FDE;&#x63A5;&#x4E0A;&#x8FDB;&#x884C;&#x5168;&#x53CC;&#x5DE5;&#x901A;&#x4FE1;&#x7684;&#x534F;&#x8BAE;&#x3002;<strong>&#x5728;WebSocket API&#x4E2D;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x548C;&#x670D;&#x52A1;&#x5668;&#x53EA;&#x9700;&#x8981;&#x5B8C;&#x6210;&#x4E00;&#x6B21;&#x63E1;&#x624B;&#xFF08;&#x4E0D;&#x662F;&#x6307;&#x5EFA;&#x7ACB;TCP&#x8FDE;&#x63A5;&#x7684;&#x90A3;&#x4E2A;&#x4E09;&#x6B21;&#x63E1;&#x624B;&#xFF0C;&#x662F;&#x6307;&#x5728;&#x5EFA;&#x7ACB;TCP&#x8FDE;&#x63A5;&#x540E;&#x4F20;&#x8F93;&#x4E00;&#x6B21;&#x63E1;&#x624B;&#x6570;&#x636E;&#xFF09;&#xFF0C;&#x4E24;&#x8005;&#x4E4B;&#x95F4;&#x5C31;&#x76F4;&#x63A5;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x6301;&#x4E45;&#x6027;&#x7684;&#x8FDE;&#x63A5;&#xFF0C;&#x5E76;&#x8FDB;&#x884C;&#x53CC;&#x5411;&#x6570;&#x636E;&#x4F20;&#x8F93;&#x3002;</strong></p>
<p><img src="../images/websocket.png" alt="websocket"></p>
<p>Websocket&#x4F7F;&#x7528;ws&#x6216;wss&#x7684;&#x7EDF;&#x4E00;&#x8D44;&#x6E90;&#x6807;&#x5FD7;&#x7B26;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;HTTPS&#xFF0C;&#x5176;&#x4E2D;wss&#x8868;&#x793A;&#x5728;TLS&#x4E4B;&#x4E0A;&#x7684;Websocket&#x3002;&#x5982;&#xFF1A;</p>
<pre><code>ws://example.com/wsapi
wss://secure.example.com/
</code></pre><p>Websocket&#x4F7F;&#x7528;&#x548C; HTTP &#x76F8;&#x540C;&#x7684; TCP &#x7AEF;&#x53E3;&#xFF0C;&#x53EF;&#x4EE5;&#x7ED5;&#x8FC7;&#x5927;&#x591A;&#x6570;&#x9632;&#x706B;&#x5899;&#x7684;&#x9650;&#x5236;&#x3002;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;Websocket&#x534F;&#x8BAE;&#x4F7F;&#x7528;80&#x7AEF;&#x53E3;&#xFF1B;&#x8FD0;&#x884C;&#x5728;TLS&#x4E4B;&#x4E0A;&#x65F6;&#xFF0C;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;443&#x7AEF;&#x53E3;&#x3002;</p>
<p><img src="../images/ws&#x4E0E;http.jpg" alt="ws&#x4E0E;http"></p>
<h4 id="&#x63E1;&#x624B;&#x534F;&#x8BAE;">&#x63E1;&#x624B;&#x534F;&#x8BAE;</h4>
<p>WebSocket &#x662F;&#x72EC;&#x7ACB;&#x7684;&#x3001;&#x521B;&#x5EFA;&#x5728; TCP &#x4E0A;&#x7684;&#x534F;&#x8BAE;&#x3002;  &#x62A5;&#x6587;</p>
<p><strong>Websocket &#x901A;&#x8FC7; HTTP/1.1 &#x534F;&#x8BAE;&#x7684;101&#x72B6;&#x6001;&#x7801;&#x8FDB;&#x884C;&#x63E1;&#x624B;&#x3002;</strong></p>
<p>&#x4E3A;&#x4E86;&#x521B;&#x5EFA;Websocket&#x8FDE;&#x63A5;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x6D4F;&#x89C8;&#x5668;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#xFF0C;&#x4E4B;&#x540E;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x56DE;&#x5E94;&#xFF0C;&#x8FD9;&#x4E2A;&#x8FC7;&#x7A0B;&#x901A;&#x5E38;&#x79F0;&#x4E3A;&#x201C;&#x63E1;&#x624B;&#x201D;&#xFF08;handshaking&#xFF09;&#x3002;</p>
<p>&#x4E00;&#x4E2A;&#x5178;&#x578B;&#x7684;Websocket&#x63E1;&#x624B;&#x8BF7;&#x6C42;&#x5982;&#x4E0B;&#xFF1A;</p>
<p>&#x5BA2;&#x6237;&#x7AEF;&#x8BF7;&#x6C42;</p>
<pre><code class="lang-http"><span class="hljs-request">GET <span class="hljs-string">/</span> HTTP/1.1</span>
<span class="hljs-attribute">Upgrade</span>: <span class="hljs-string">websocket</span>
<span class="hljs-attribute">Connection</span>: <span class="hljs-string">Upgrade</span>
<span class="hljs-attribute">Host</span>: <span class="hljs-string">example.com</span>
<span class="hljs-attribute">Origin</span>: <span class="hljs-string">http://example.com</span>
<span class="hljs-attribute">Sec-WebSocket-Key</span>: <span class="hljs-string">sN9cRrP/n9NdMgdcy2VJFQ==</span>
<span class="hljs-attribute">Sec-WebSocket-Version</span>: <span class="hljs-string">13</span>
</code></pre>
<p>&#x670D;&#x52A1;&#x5668;&#x56DE;&#x5E94;</p>
<pre><code class="lang-http"><span class="hljs-status">HTTP/1.1 <span class="hljs-number">101</span> Switching Protocols</span>
<span class="hljs-attribute">Upgrade</span>: <span class="hljs-string">websocket</span>
<span class="hljs-attribute">Connection</span>: <span class="hljs-string">Upgrade</span>
<span class="hljs-attribute">Sec-WebSocket-Accept</span>: <span class="hljs-string">fFBooB7FAkLlXgRSz0BT3v4hq5s=</span>
<span class="hljs-attribute">Sec-WebSocket-Location</span>: <span class="hljs-string">ws://example.com/</span>
</code></pre>
<ul>
<li>Connection&#x5FC5;&#x987B;&#x8BBE;&#x7F6E;Upgrade&#xFF0C;&#x8868;&#x793A;&#x5BA2;&#x6237;&#x7AEF;&#x5E0C;&#x671B;&#x8FDE;&#x63A5;&#x5347;&#x7EA7;&#x3002;</li>
<li>Upgrade&#x5B57;&#x6BB5;&#x5FC5;&#x987B;&#x8BBE;&#x7F6E;Websocket&#xFF0C;&#x8868;&#x793A;&#x5E0C;&#x671B;&#x5347;&#x7EA7;&#x5230;Websocket&#x534F;&#x8BAE;&#x3002;</li>
<li>Sec-WebSocket-Key&#x662F;&#x968F;&#x673A;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x4F1A;&#x7528;&#x8FD9;&#x4E9B;&#x6570;&#x636E;&#x6765;&#x6784;&#x9020;&#x51FA;&#x4E00;&#x4E2A;SHA-1&#x7684;&#x4FE1;&#x606F;&#x6458;&#x8981;&#x3002;&#x628A;&#x201C;Sec-WebSocket-Key&#x201D;&#x52A0;&#x4E0A;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x5B57;&#x7B26;&#x4E32;&#x201C;258EAFA5-E914-47DA-95CA-C5AB0DC85B11&#x201D;&#xFF0C;&#x7136;&#x540E;&#x8BA1;&#x7B97;SHA-1&#x6458;&#x8981;&#xFF0C;&#x4E4B;&#x540E;&#x8FDB;&#x884C;BASE-64&#x7F16;&#x7801;&#xFF0C;&#x5C06;&#x7ED3;&#x679C;&#x505A;&#x4E3A;&#x201C;Sec-WebSocket-Accept&#x201D;&#x5934;&#x7684;&#x503C;&#xFF0C;&#x8FD4;&#x56DE;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x3002;&#x5982;&#x6B64;&#x64CD;&#x4F5C;&#xFF0C;<strong>&#x53EF;&#x4EE5;&#x5C3D;&#x91CF;&#x907F;&#x514D;&#x666E;&#x901A;HTTP&#x8BF7;&#x6C42;&#x88AB;&#x8BEF;&#x8BA4;&#x4E3A;Websocket&#x534F;&#x8BAE;&#x3002;</strong></li>
<li>Sec-WebSocket-Version &#x8868;&#x793A;&#x652F;&#x6301;&#x7684;Websocket&#x7248;&#x672C;&#x3002;RFC6455&#x8981;&#x6C42;&#x4F7F;&#x7528;&#x7684;&#x7248;&#x672C;&#x662F;13&#xFF0C;&#x4E4B;&#x524D;&#x8349;&#x6848;&#x7684;&#x7248;&#x672C;&#x5747;&#x5E94;&#x5F53;&#x5F03;&#x7528;&#x3002;</li>
<li>Origin&#x5B57;&#x6BB5;&#x662F;&#x53EF;&#x9009;&#x7684;&#xFF0C;&#x901A;&#x5E38;&#x7528;&#x6765;&#x8868;&#x793A;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x53D1;&#x8D77;&#x6B64;Websocket&#x8FDE;&#x63A5;&#x6240;&#x5728;&#x7684;&#x9875;&#x9762;&#xFF0C;&#x7C7B;&#x4F3C;&#x4E8E;Referer&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x4E0E;Referer&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;Origin&#x53EA;&#x5305;&#x542B;&#x4E86;&#x534F;&#x8BAE;&#x548C;&#x4E3B;&#x673A;&#x540D;&#x79F0;&#x3002;</li>
<li>&#x5176;&#x4ED6;&#x4E00;&#x4E9B;&#x5B9A;&#x4E49;&#x5728;HTTP&#x534F;&#x8BAE;&#x4E2D;&#x7684;&#x5B57;&#x6BB5;&#xFF0C;&#x5982;Cookie&#x7B49;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x5728;Websocket&#x4E2D;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<h4 id="&#x4F18;&#x70B9;">&#x4F18;&#x70B9;</h4>
<ul>
<li><strong>&#x8F83;&#x5C11;&#x7684;&#x63A7;&#x5236;&#x5F00;&#x9500;&#x3002;</strong>&#x5728;&#x8FDE;&#x63A5;&#x521B;&#x5EFA;&#x540E;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x548C;&#x5BA2;&#x6237;&#x7AEF;&#x4E4B;&#x95F4;&#x4EA4;&#x6362;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x7528;&#x4E8E;&#x534F;&#x8BAE;&#x63A7;&#x5236;&#x7684;&#x6570;&#x636E;&#x5305;&#x5934;&#x90E8;&#x76F8;&#x5BF9;&#x8F83;&#x5C0F;&#x3002;&#x5728;&#x4E0D;&#x5305;&#x542B;&#x6269;&#x5C55;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x5BF9;&#x4E8E;&#x670D;&#x52A1;&#x5668;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x6B64;&#x5934;&#x90E8;&#x5927;&#x5C0F;&#x53EA;&#x6709;2&#x81F3;10&#x5B57;&#x8282;&#xFF08;&#x548C;&#x6570;&#x636E;&#x5305;&#x957F;&#x5EA6;&#x6709;&#x5173;&#xFF09;&#xFF1B;&#x5BF9;&#x4E8E;&#x5BA2;&#x6237;&#x7AEF;&#x5230;&#x670D;&#x52A1;&#x5668;&#x7684;&#x5185;&#x5BB9;&#xFF0C;&#x6B64;&#x5934;&#x90E8;&#x8FD8;&#x9700;&#x8981;&#x52A0;&#x4E0A;&#x989D;&#x5916;&#x7684;4&#x5B57;&#x8282;&#x7684;&#x63A9;&#x7801;&#x3002;<strong>&#x76F8;&#x5BF9;&#x4E8E;HTTP&#x8BF7;&#x6C42;&#x6BCF;&#x6B21;&#x90FD;&#x8981;&#x643A;&#x5E26;&#x5B8C;&#x6574;&#x7684;&#x5934;&#x90E8;&#xFF0C;&#x6B64;&#x9879;&#x5F00;&#x9500;&#x663E;&#x8457;&#x51CF;&#x5C11;&#x4E86;&#x3002;</strong></li>
<li><strong>&#x66F4;&#x5F3A;&#x7684;&#x5B9E;&#x65F6;&#x6027;&#x3002;</strong>&#x7531;&#x4E8E;&#x534F;&#x8BAE;&#x662F;&#x5168;&#x53CC;&#x5DE5;&#x7684;&#xFF0C;&#x6240;&#x4EE5;<strong>&#x670D;&#x52A1;&#x5668;&#x53EF;&#x4EE5;&#x968F;&#x65F6;&#x4E3B;&#x52A8;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x4E0B;&#x53D1;&#x6570;&#x636E;&#x3002;</strong>&#x76F8;&#x5BF9;&#x4E8E;HTTP&#x8BF7;&#x6C42;&#x9700;&#x8981;&#x7B49;&#x5F85;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8D77;&#x8BF7;&#x6C42;&#x670D;&#x52A1;&#x7AEF;&#x624D;&#x80FD;&#x54CD;&#x5E94;&#xFF0C;&#x5EF6;&#x8FDF;&#x660E;&#x663E;&#x66F4;&#x5C11;&#xFF1B;&#x5373;&#x4F7F;&#x662F;&#x548C;Comet&#x7B49;&#x7C7B;&#x4F3C;&#x7684;&#x957F;&#x8F6E;&#x8BE2;&#x6BD4;&#x8F83;&#xFF0C;&#x5176;&#x4E5F;&#x80FD;&#x5728;&#x77ED;&#x65F6;&#x95F4;&#x5185;&#x66F4;&#x591A;&#x6B21;&#x5730;&#x4F20;&#x9012;&#x6570;&#x636E;&#x3002;</li>
<li><strong>&#x4FDD;&#x6301;&#x8FDE;&#x63A5;&#x72B6;&#x6001;&#x3002;</strong>&#x4E0E;HTTP&#x4E0D;&#x540C;&#x7684;&#x662F;&#xFF0C;Websocket&#x9700;&#x8981;&#x5148;&#x521B;&#x5EFA;&#x8FDE;&#x63A5;&#xFF0C;&#x8FD9;&#x5C31;&#x4F7F;&#x5F97;&#x5176;&#x6210;&#x4E3A;&#x4E00;&#x79CD;&#x6709;&#x72B6;&#x6001;&#x7684;&#x534F;&#x8BAE;&#xFF0C;&#x4E4B;&#x540E;&#x901A;&#x4FE1;&#x65F6;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x90E8;&#x5206;&#x72B6;&#x6001;&#x4FE1;&#x606F;&#x3002;&#x800C;HTTP&#x8BF7;&#x6C42;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5728;&#x6BCF;&#x4E2A;&#x8BF7;&#x6C42;&#x90FD;&#x643A;&#x5E26;&#x72B6;&#x6001;&#x4FE1;&#x606F;&#xFF08;&#x5982;&#x8EAB;&#x4EFD;&#x8BA4;&#x8BC1;&#x7B49;&#xFF09;&#x3002;
  &#x66F4;&#x597D;&#x7684;&#x4E8C;&#x8FDB;&#x5236;&#x652F;&#x6301;&#x3002;<strong>Websocket&#x5B9A;&#x4E49;&#x4E86;&#x4E8C;&#x8FDB;&#x5236;&#x5E27;&#xFF0C;&#x76F8;&#x5BF9;HTTP&#xFF0C;&#x53EF;&#x4EE5;&#x66F4;&#x8F7B;&#x677E;&#x5730;&#x5904;&#x7406;&#x4E8C;&#x8FDB;&#x5236;&#x5185;&#x5BB9;&#x3002;</strong></li>
<li><strong>&#x53EF;&#x4EE5;&#x652F;&#x6301;&#x6269;&#x5C55;</strong>&#x3002;Websocket&#x5B9A;&#x4E49;&#x4E86;&#x6269;&#x5C55;&#xFF0C;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x6269;&#x5C55;&#x534F;&#x8BAE;&#x3001;&#x5B9E;&#x73B0;&#x90E8;&#x5206;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x5B50;&#x534F;&#x8BAE;&#x3002;&#x5982;&#x90E8;&#x5206;&#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;&#x538B;&#x7F29;&#x7B49;&#x3002;</li>
<li><strong>&#x66F4;&#x597D;&#x7684;&#x538B;&#x7F29;&#x6548;&#x679C;</strong>&#x3002;&#x76F8;&#x5BF9;&#x4E8E;HTTP&#x538B;&#x7F29;&#xFF0C;Websocket&#x5728;&#x9002;&#x5F53;&#x7684;&#x6269;&#x5C55;&#x652F;&#x6301;&#x4E0B;&#xFF0C;&#x53EF;&#x4EE5;&#x6CBF;&#x7528;&#x4E4B;&#x524D;&#x5185;&#x5BB9;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x5728;&#x4F20;&#x9012;&#x7C7B;&#x4F3C;&#x7684;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x663E;&#x8457;&#x5730;&#x63D0;&#x9AD8;&#x538B;&#x7F29;&#x7387;&#x3002;</li>
<li><strong>&#x6CA1;&#x6709;&#x540C;&#x6E90;&#x9650;&#x5236;&#xFF0C;&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x4E0E;&#x4EFB;&#x610F;&#x670D;&#x52A1;&#x5668;&#x901A;&#x4FE1;&#x3002;</strong></li>
<li><strong>&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x6587;&#x672C;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x53D1;&#x9001;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x636E;&#x3002;</strong></li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C09-IM/AboutIM.html" class="navigation navigation-prev " aria-label="Previous page: 即时通信简介"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C09-IM/SocketIO.html" class="navigation navigation-next " aria-label="Next page: Socket.IO"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
